package com.example.contentprovider0216.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.contentprovider0216.db.UserDatabaseHelper;
import com.example.contentprovider0216.pojo.User;
import com.example.contentprovider0216.utils.Constants;

import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements IUserDao {

    private  final UserDatabaseHelper mUserDatabaseHelper;

    public UserDaoImpl(Context context){
        mUserDatabaseHelper=new UserDatabaseHelper(context);
    }
    @Override
    public long addUser(User user) {
        SQLiteDatabase db=mUserDatabaseHelper.getWritableDatabase();
        ContentValues values = user2Values(user);
        long result=db.insert(Constants.TABLE_NAME,null,values);
        //db.close();
        return result;
    }

    @Override
    public int delUserBuId(int id) {
        SQLiteDatabase db=mUserDatabaseHelper.getReadableDatabase();
        int result=db.delete(Constants.TABLE_NAME,Constants.FIELD_ID,new String[]{id+""});
        db.close();
        return result;
    }

    @Override
    public int updateUser(User user) {
        SQLiteDatabase db=mUserDatabaseHelper.getWritableDatabase();
        ContentValues values = user2Values(user);

        //根据id来更新内容
        int result=db.update(Constants.TABLE_NAME,values,Constants.FIELD_ID,new String[]{user.get_id()+""});
        db.close();
        return result;
    }

    private ContentValues user2Values(User user) {
        ContentValues values = new ContentValues();
        values.put(Constants.FIELD_USER_NAME, user.getUserName());
        values.put(Constants.FIELD_AGE, user.getAge());
        values.put(Constants.FIELD_PASSWORD, user.getPassword());
        values.put(Constants.FIELD_SEX, user.getSex());
        return values;
    }

    @Override
    public User getUserById(int id) {
        SQLiteDatabase db=mUserDatabaseHelper.getReadableDatabase();
        //可用sql语句
        String sql="select * from "+Constants.TABLE_NAME+" where "+Constants.FIELD_ID+"=?";
        Cursor cursor=db.rawQuery(sql,new String[]{id+""});
        User user=null;
        if (cursor.moveToNext()){
            user=cursor2User(cursor);
        }
        db.close();
        return user;
    }

    private User cursor2User(Cursor cursor) {
        User user=new User();
        int userId= cursor.getInt(cursor.getColumnIndex(Constants.FIELD_ID));
        user.set_id(userId);
        String userName= cursor.getString(cursor.getColumnIndex(Constants.FIELD_USER_NAME));
        user.setUserName(userName);
        String userSex= cursor.getString(cursor.getColumnIndex(Constants.FIELD_SEX));
        String userPassword= cursor.getString(cursor.getColumnIndex(Constants.FIELD_PASSWORD));
        int userAge= cursor.getInt(cursor.getColumnIndex(Constants.FIELD_AGE));
        user.setAge(userAge);
        user.setPassword(userPassword);
        user.setSex(userSex);
        return user;
    }

    @Override
    public List<User> listAllUser() {
        SQLiteDatabase db=mUserDatabaseHelper.getReadableDatabase();
        Cursor cursor=db.query(Constants.TABLE_NAME,null,null,null,
                null,null,null,null);
        List<User>users=new ArrayList<>();
        while (cursor.moveToNext()){
            User user=cursor2User(cursor);
            users.add(user);
        }
        db.close();
        return users;
    }
}
